<template>
  <div class="newList">
    <div class="newList-title">
      <span class="newList-title-p1 reset" v-if="val.title">{{val.title}}</span>
      <span class="newList-title-p1 reset" v-else>{{val.menuname}}</span>
    </div>
    <van-list
      v-if="listAll.length>0"
      v-model="loading"
      :finished-text="finishedText"
      :finished="finished"
      @load="onLoad"
      class="news-box"
    >
      <van-cell class="news-item" v-for="(i,index) in list" :data="index" :key="index">
        <newsItem :i="i"></newsItem>
      </van-cell>
    </van-list>
    <nullList v-if="nullListShow"></nullList>
  </div>
</template>

<script type="text/ecmascript-6">
import { getchannelinfolist } from "../../api/sort";
import newsItem from "./newstLi";
import nullList from "../nulllist/nullList";
export default {
  props: ["val"],
  components: {
    newsItem,
    nullList
  },
  data() {
    return {
      titleConfigs: this.titleConfig || {},
      list: [],
      finished: false,
      finishedText: "",
      listAll: [], //所有新闻，规定只给50条，当所有新闻,自己做分页功能
      error: false,
      loading: false,
      nullListShow: false,
      page: 0,
      pageSize: 10
    };
  },

  methods: {
    onLoad() {
      // 异步更新数据
      setTimeout(() => {
         let begin = this.page * this.pageSize;
        let end = (this.page + 1) * this.pageSize;
        for (let i = begin; i < end; i++) {
          if (this.listAll[i]) {
             this.list.push(this.listAll[i]);
          }
        }
        this.page++;
        // 加载状态结束
        this.loading = false;
        // 数据全部加载完成
        if (this.list.length >= this.listAll.length) {
          this.finished = true;
          if (this.listAll.length > 4) {
            this.finishedText = "No more";
          }
        }
      }, 500);
    },
    //获取新闻信息
    getchannelist() {
      let obj = {};
      obj.channel = this.val.channel;
      obj.cid = this.val.cid;
      
      getchannelinfolist(obj).then(res => {
        if (res.code === "0000") {
          this.listAll = res.data || [];
          if (this.listAll.length == 0) {
            this.nullListShow = true;
          }
        }
      });
    }
  },
  created() {
    this.getchannelist();
  }
};
</script>

<style scoped lang="less">
@import url("./news.less");
</style>
